package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.WindowManager
import com.bumptech.glide.Glide
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.PhoneBean
import hlhj.fhp.supreme.javabean.UserInfoBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_my_vip_aty.*

class MyVipAty : BaseAty() {
    private var num="12"
    private var money=""
    private val CHARGE_VIP=1
    private var singleMonth=""
    override fun getContentId(): Int {
        return R.layout.activity_my_vip_aty
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
    }
    override fun initView() {
        OkGo.post<UserInfoBean>(Urls.BaseUrl+Urls.UserInfo)
                .execute(object :JsonCallBack<UserInfoBean>(this){
                    override fun onSuccess(response: Response<UserInfoBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            Glide.with(this@MyVipAty).load(bean?.data.avatar).into(userIcon)
                            userNick.text=bean?.data.user_nickname
                            if (bean?.data.end_time.equals("0")){
                                tvStatus.text="您当前未开通读书会员"
                                /*vipTime.text="开通会员的优势说明开通会员的优势说明开通会员的优势\n" +
                                        "说明开通会员的优势说明说明开通会员的优势说明说明开\n" +
                                        "通会员的优势说明说明开通会员的优势说明。"*/
                            }else{
                                if (bean?.data.end_time.toLong()<(System.currentTimeMillis()/1000)){
                                    tvStatus.text="您的会员已过期"
                                    vipTime.text="过期时间为${MyUtils.timeStampToStrYMD(bean?.data.end_time.toLong())}"
                                }else{
                                    tvStatus.text="您已开通会员"
                                    vipTime.text="会员有效期:  ${MyUtils.timeStampToStrYMD(bean?.data.end_time.toLong())}"
                                }
                            }

                        }
                    }
                })

        getMonthPay()
    }

    private fun getMonthPay() {
        OkGo.post<PhoneBean>(Urls.BaseUrl+Urls.PHONE)
                .params("type","member")
                .execute(object :JsonCallBack<PhoneBean>(this@MyVipAty){
                    override fun onSuccess(response: Response<PhoneBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            singleMonth=bean?.data.member
                            money="￥"+(bean?.data.member.toInt()*12).toString()
                            tv1_2.text="￥${bean?.data.member}"
                            tv2_2.text="￥${(bean?.data.member.toInt())*3}"
                            tv3_2.text="￥${(bean?.data.member.toInt())*6}"
                            tv4_2.text="￥${(bean?.data.member.toInt()*12)}"
                        }
                    }
                })
    }

    override fun initListener() {
        btExit.setOnClickListener {
            finish()
        }
        lo1.setOnClickListener {
            num="1"
            money="￥${1*(singleMonth.toInt())}"
            lo1.setBackgroundResource(R.drawable.getcash_button)
            lo3.setBackgroundResource(R.drawable.vip_border)
            lo6.setBackgroundResource(R.drawable.vip_border)
            lo12.setBackgroundResource(R.drawable.vip_border)
            tv1_1.setTextColor(resources.getColor(R.color.white))
            tv1_2.setTextColor(resources.getColor(R.color.white))
            tv2_1.setTextColor(resources.getColor(R.color.text_333))
            tv2_2.setTextColor(resources.getColor(R.color.them_yellow))
            tv3_1.setTextColor(resources.getColor(R.color.text_333))
            tv3_2.setTextColor(resources.getColor(R.color.them_yellow))
            tv4_1.setTextColor(resources.getColor(R.color.text_333))
            tv4_2.setTextColor(resources.getColor(R.color.them_yellow))
        }
        lo3.setOnClickListener {
            num="3"
            money="￥${3*(singleMonth.toInt())}"
            lo1.setBackgroundResource(R.drawable.vip_border)
            lo3.setBackgroundResource(R.drawable.getcash_button)
            lo6.setBackgroundResource(R.drawable.vip_border)
            lo12.setBackgroundResource(R.drawable.vip_border)
            tv2_1.setTextColor(resources.getColor(R.color.white))
            tv2_2.setTextColor(resources.getColor(R.color.white))
            tv1_1.setTextColor(resources.getColor(R.color.text_333))
            tv1_2.setTextColor(resources.getColor(R.color.them_yellow))
            tv3_1.setTextColor(resources.getColor(R.color.text_333))
            tv3_2.setTextColor(resources.getColor(R.color.them_yellow))
            tv4_1.setTextColor(resources.getColor(R.color.text_333))
            tv4_2.setTextColor(resources.getColor(R.color.them_yellow))
        }
        lo6.setOnClickListener {
            num="6"
            money="￥${6*(singleMonth.toInt())}"
            lo1.setBackgroundResource(R.drawable.vip_border)
            lo6.setBackgroundResource(R.drawable.getcash_button)
            lo3.setBackgroundResource(R.drawable.vip_border)
            lo12.setBackgroundResource(R.drawable.vip_border)
            tv3_1.setTextColor(resources.getColor(R.color.white))
            tv3_2.setTextColor(resources.getColor(R.color.white))
            tv1_1.setTextColor(resources.getColor(R.color.text_333))
            tv1_2.setTextColor(resources.getColor(R.color.them_yellow))
            tv2_1.setTextColor(resources.getColor(R.color.text_333))
            tv2_2.setTextColor(resources.getColor(R.color.them_yellow))
            tv4_1.setTextColor(resources.getColor(R.color.text_333))
            tv4_2.setTextColor(resources.getColor(R.color.them_yellow))
        }
        lo12.setOnClickListener {
            num="12"
            money="￥${12*(singleMonth.toInt())}"

            lo1.setBackgroundResource(R.drawable.vip_border)
            lo12.setBackgroundResource(R.drawable.getcash_button)
            lo3.setBackgroundResource(R.drawable.vip_border)
            lo6.setBackgroundResource(R.drawable.vip_border)
            tv4_1.setTextColor(resources.getColor(R.color.white))
            tv4_2.setTextColor(resources.getColor(R.color.white))
            tv1_1.setTextColor(resources.getColor(R.color.text_333))
            tv1_2.setTextColor(resources.getColor(R.color.them_yellow))
            tv2_1.setTextColor(resources.getColor(R.color.text_333))
            tv2_2.setTextColor(resources.getColor(R.color.them_yellow))
            tv3_1.setTextColor(resources.getColor(R.color.text_333))
            tv3_2.setTextColor(resources.getColor(R.color.them_yellow))
        }


        btCommit.setOnClickListener {
            if (num.equals("0")){
                MyUtils.toast("请选择月数")
            }else{
                val intent = Intent(this, ChageVipAty::class.java)
                intent.putExtra("money",money)
                intent.putExtra("num",num)
                startActivityForResult(intent,CHARGE_VIP)
            }
        }

        custom.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(s: Editable?) {
                if (s.toString().equals("")){
                    num="0"
                    money="￥0"
                }else{
                    num = s.toString()
                    money="￥${num.toInt()*(singleMonth.toInt())}"
                    tv5_2.text="￥${num.toInt()*(singleMonth.toInt())}"
                }

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
        })


    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        if (requestCode==CHARGE_VIP){
            finish()
        }
    }
}
